// 获取url中参数
export function getQueryString(url) {
    const obj = {}
    if (url.indexOf('?') !== -1) {
        const str = url.split('?')[1]
        const strs = str.split('&')
        strs.map((item, i) => {
            const arr = strs[i].split('=')
            obj[arr[0]] = arr[1]
        })
    }
    return obj
}

// 进入路由的判断
export const isLogin = (nextState, replaceState) => {
    console.log(nextState.location.query)
    // 路由登录
    if(nextState.location.query && (nextState.location.query.ticket || nextState.location.query.key)) {
        sessionStorage.setItem('token',nextState.location.query.ticket || nextState.query.key)
    }
    const token = sessionStorage.getItem('token')
    if (!token) { // 没有token，那就返回首页
        replaceState('/login')
    }
}


// 异步请求需要走redux的方式，并将函数柯里化(curring)
export const createAjaxAction = (createdApi, startAction, endAction) => (request = {}, resolve, reject, config) => (dispatch) => {
    if (startAction) dispatch(startAction({ req: request, res: {} }))
    const _resolve = (response) => {
        if (endAction) dispatch(endAction({ req: request, res: response }))
        if (resolve) resolve(response)
    }
    return createdApi(request, _resolve, reject, config)
}
